#include "TexturedCube.h"
#include "SOIL.h"


TexturedCube::TexturedCube(float width, float length, float height, std::wstring fileName):
	m_Width(width),
	m_Length(length),
	m_Height(height),
	m_DiffuseTextureFileName(fileName)
{

	
}

TexturedCube::~TexturedCube(void)
{
}

void TexturedCube::Init()
{
	float w = m_Width/2;
	float l = m_Length/2;
	float h = m_Height;

	m_ps[0].setValues(-w,-l,0);
	m_ps[1].setValues(-w,l,0);
	m_ps[2].setValues(w,l,0);
	m_ps[3].setValues(w,-l,0);
	
	m_ps[4].setValues(-w,-l,h);
	m_ps[5].setValues(-w,l,h);	
	m_ps[6].setValues(w,l,h);
	m_ps[7].setValues(w,-l,h);

	m_uvs[0].setValues(0,0);
	m_uvs[1].setValues(1,0);
	m_uvs[2].setValues(1,1);
	m_uvs[3].setValues(0,1);
	

	std::string filename(m_DiffuseTextureFileName.begin(),m_DiffuseTextureFileName.end());
	m_TextureID = SOIL_load_OGL_texture
	(
		filename.c_str(),
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
	);
}

void TexturedCube::Vertex(float3& p, float2& uv)
{
	glTexCoord2f(uv.u,uv.v);
	glVertex3f(p.x,p.y,p.z);
}

void TexturedCube::Draw()
{
	glBindTexture(GL_TEXTURE_2D,m_TextureID);
	glEnable(GL_TEXTURE_2D);

	glBegin(GL_QUADS);
	glColor3ub(255,255,255);
	Vertex(m_ps[0],m_uvs[0]);
	Vertex(m_ps[1],m_uvs[1]);
	Vertex(m_ps[2],m_uvs[2]);
	Vertex(m_ps[3],m_uvs[3]);
	
	Vertex(m_ps[4],m_uvs[0]);
	Vertex(m_ps[5],m_uvs[1]);
	Vertex(m_ps[6],m_uvs[2]);
	Vertex(m_ps[7],m_uvs[3]);
	
	Vertex(m_ps[0],m_uvs[0]);
	Vertex(m_ps[1],m_uvs[1]);
	Vertex(m_ps[5],m_uvs[2]);
	Vertex(m_ps[4],m_uvs[3]);
	
	Vertex(m_ps[2],m_uvs[0]);
	Vertex(m_ps[3],m_uvs[1]);
	Vertex(m_ps[7],m_uvs[2]);
	Vertex(m_ps[6],m_uvs[3]);
	
	Vertex(m_ps[1],m_uvs[0]);
	Vertex(m_ps[2],m_uvs[1]);
	Vertex(m_ps[6],m_uvs[2]);
	Vertex(m_ps[5],m_uvs[3]);
	
	Vertex(m_ps[0],m_uvs[0]);
	Vertex(m_ps[3],m_uvs[1]);
	Vertex(m_ps[7],m_uvs[2]);
	Vertex(m_ps[4],m_uvs[3]);
	glEnd();
	glDisable(GL_TEXTURE_2D);
}
